package org.example;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

public class Swing_3 {
    public static void main(String[] args) throws IOException {
        UserDaoImpl userDao = new UserDaoImpl();
        JFrame jFrame = new JFrame("四川师范大学学生管理系统");//创建窗口
        jFrame.setSize(600, 350);//设置窗口大小
        InputStream im = Demo2.class.getClassLoader().getResourceAsStream("1.png");
        BufferedImage image = ImageIO.read(im);
        jFrame.setIconImage(image);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //系统标题 title
        JLabel title = new JLabel("四川师范大学生管理系统", JLabel.CENTER);
        title.setPreferredSize(new Dimension(0, 80));//位置设置
        title.setFont(new Font("宋体", Font.BOLD, 30));//文体设置
        jFrame.add(title, BorderLayout.NORTH);
        //通用文字对象
        Font font = new Font("楷体", Font.PLAIN, 20);
        //账号文字对象  labelUser  labelPass
        JLabel labelUser = new JLabel("用户名:");
        JLabel labelPass = new JLabel("密码:");
        labelUser.setFont(font);
        labelPass.setFont(font);

        //按钮:save
        JButton save1 = new JButton("确定");
        save1.setFont(font);
        JButton save2 = new JButton("保存");
        save2.setFont(font);
        //输入框对象 username
        JTextField username = new JTextField();
        username.setPreferredSize(new Dimension(200, 30));
        //密码输入框对象 password
        JPasswordField password = new JPasswordField();
        password.setPreferredSize(new Dimension(200, 30));
        //布局对象  layout
        SpringLayout layout = new SpringLayout();
        JPanel center = new JPanel(layout);
        center.add(labelPass);
        center.add(labelUser);
        center.add(username);
        center.add(password);
        center.add(save1);
        center.add(save2);

        int usernameW = Spring.width(labelUser).getValue();
        int userPassH = Spring.height(labelUser).getValue();
        int passW = Spring.width(labelPass).getValue();
        int sum = (usernameW + Spring.width(username).getValue() + 20) / 2;
        //用户文本框的定位
        layout.putConstraint(SpringLayout.WEST, labelUser, -sum, SpringLayout.HORIZONTAL_CENTER, center);
        //用户输入框
        layout.putConstraint(SpringLayout.WEST, username, usernameW + 20, SpringLayout.WEST, labelUser);
        //密码文本框的位置布局
        layout.putConstraint(SpringLayout.NORTH, labelPass, userPassH + 30, SpringLayout.NORTH, labelUser);
        layout.putConstraint(SpringLayout.WEST, labelPass, 0, SpringLayout.WEST, labelUser);
        //密码输入框的位置布局
        layout.putConstraint(SpringLayout.NORTH, password, userPassH + 30, SpringLayout.NORTH, labelUser);
        layout.putConstraint(SpringLayout.WEST, password, usernameW + 20, SpringLayout.WEST, labelUser);
        //save1的位置布局
        layout.putConstraint(SpringLayout.WEST, save1, -30, SpringLayout.WEST, labelUser);
        layout.putConstraint(SpringLayout.NORTH, save1, userPassH + 30 + 70, SpringLayout.NORTH, labelUser);
        //save2的位置布局
        layout.putConstraint(SpringLayout.EAST, save2, +30, SpringLayout.EAST, password);
        layout.putConstraint(SpringLayout.NORTH, save2, userPassH + 30 + 70, SpringLayout.NORTH, labelUser);
        jFrame.add(center);
        jFrame.setVisible(true);

        save1.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = username.getText();
                String pass = password.getText();//表示方法过期，有更好的方法，但不表示不可用
//                System.out.println(name + pass);

                boolean b = userDao.login(name, pass);
                if (b) {
                    JOptionPane.showMessageDialog(jFrame, "允许登录");
                    jFrame.setVisible(false);
//                    Swing_3 swing_3 = new Swing_3();
//                    jFrame.getContentPane().removeAll();
                    try {
                        Swing2_1.main();
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                } else if (username.getText().equals("")) JOptionPane.showMessageDialog(jFrame, "用户名不可为空！");
                else if (password.getText().equals("")) JOptionPane.showMessageDialog(jFrame, "密码不可为空！");
                else JOptionPane.showMessageDialog(jFrame, "输入有误");
            }
        });
    }
}
//《》泛型：泛指某一种数据类型，表明这个集合只能存这一种数据类型

